using Cinemachine;
using UnityEngine;

public class PlayerFX : EntityFX
{


    [Header("Screen shake FX")]
    private CinemachineImpulseSource screenShake;
    [SerializeField] private float shakeMultiplier;
    public Vector3 shakeSwordImpact;
    public Vector3 shakeHighDamage;

    [Header("After image fx")]
    [SerializeField] private GameObject afterImagePrefab;
    [SerializeField] private float colorLooseRate;
    [SerializeField] private float afterImageCooldown;
    private float afterImageCooldownTimer;

    [Header("Sword FX")]
    [SerializeField] private GameObject criticalSwordFx;

    [Space]
    [SerializeField] private ParticleSystem dustFx;

    protected override void Start()
    {
        base.Start();

        screenShake = GetComponent<CinemachineImpulseSource>();
    }

    private void Update()
    {
        afterImageCooldownTimer -= Time.deltaTime;
    }

    public void ScreenShake(Vector3 _shakePower)
    {
        screenShake.m_DefaultVelocity = new Vector3(_shakePower.x * player.facingDir, _shakePower.y) * shakeMultiplier;
        screenShake.GenerateImpulse();
    }

    public void CreateAfterImage()
    {
        if (afterImageCooldownTimer < 0)
        {
            afterImageCooldownTimer = afterImageCooldown;
            GameObject newAfterImage = Instantiate(afterImagePrefab, transform.position, transform.rotation);
            newAfterImage.GetComponent<AfterImageFX>().SetupAfterImage(colorLooseRate, sr.sprite);
        }
    }

    public void PlayDustFX()
    {
        if (dustFx != null)
            dustFx.Play();
    }

    public void CreateSwordFx()
    {
        Player player = PlayerManager.instance.player;

        bool firstAttack = player.GetComponent<Player>().primaryAttack.comboCounter == 0;

        Transform transform = player.transform;

        Vector3 swordFxPosition = new Vector3(transform.position.x + 1.5f * player.facingDir, transform.position.y, transform.position.z);

        if (firstAttack)
        {
            GameObject newCriticalSword = Instantiate(criticalSwordFx, swordFxPosition, transform.rotation);
            newCriticalSword.transform.Rotate(new Vector3(0, 0, -200 * player.facingDir));

            Destroy(newCriticalSword, .5f);
        }
    }
}
